package com.jyw.system.utils;

import com.google.common.collect.Lists;
import com.jyw.common.constant.CacheConstants;
import com.jyw.common.core.domain.entity.SysDept;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.core.redis.RedisCache;
import com.jyw.common.utils.StringUtils;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysDeptService;
import com.jyw.system.service.ISysUserService;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 沈峻
 * @ClassName DeptUtils
 * @Description 科室工具类
 * @Date 2023/8/24 17:41
 **/
public class DeptUtils {

    /**
     * 部门ID转部门名称
     * @param deptId 部门ID (可以是字符串 1,2,3 也可以是Long类型)
     * @return
     */
    public static String deptIdToName(Object deptId){
        if (ObjectUtils.isEmpty(deptId)) {
            return null;
        }
        List<SysDept> list = cacheList();
        //判断用户Id是否为字符串
        if(deptId instanceof String){
            String idStr = (String) deptId;
            if(!StringUtils.isEmpty(idStr)){
                return Arrays.stream(idStr.split(",")).map(data -> {
                    Optional<SysDept> optional = list.stream().filter(dept -> dept.getDeptId().toString().equals(data)).findFirst();
                    if (optional.isPresent()) {
                        return optional.get().getDeptName();
                    } else {
                        return data;
                    }
                }).collect(Collectors.joining(","));
            }
        } else if (deptId instanceof Long) {
            Long idL = (Long) deptId;
            Optional<SysDept> optional = list.stream().filter(dept -> dept.getDeptId().equals(idL)).findFirst();
            if (optional.isPresent()){
                return optional.get().getDeptName();
            }else{
                return idL.toString();
            }
        }else if (deptId instanceof Integer) {
            Integer idL = (Integer) deptId;
            Optional<SysDept> optional = list.stream().filter(dept -> dept.getDeptId().equals(idL)).findFirst();
            if (optional.isPresent()){
                return optional.get().getDeptName();
            }else{
                return idL.toString();
            }
        }
        return null;
    }

    /**
     * 清除用户工具类使用的用户缓存
     */
    public static void cleanCache(){
        getRedisCache().deleteObject(CacheConstants.DEPT_CACHE_KEY);
    }

    /**
     * 获取部门列表缓存
     */
    public static List<SysDept> cacheList(){
        RedisCache redisCache = getRedisCache();
        if (redisCache.hasKey(CacheConstants.DEPT_CACHE_KEY)) {
            //存在部门缓存列表
            return redisCache.getCacheList(CacheConstants.DEPT_CACHE_KEY);
        }else {
            //从数据库中索引
            List<SysDept> list = getDeptService().list();
            redisCache.setCacheList(CacheConstants.DEPT_CACHE_KEY,list);
            return list;
        }
    }

    /**
     * 获取部门接口服务
     * @return
     */
    private static ISysDeptService getDeptService(){
        return SpringUtils.getBean(ISysDeptService.class);
    }
    /**
     * 获取Redis操作对象
     */
    private static RedisCache getRedisCache(){
        return SpringUtils.getBean(RedisCache.class);
    }

    /**
     * 通过部门id获取上级名称
     * @param deptId
     * @return
     */
    public static String deptIdToPathName(Long deptId) {
        List<SysDept> sysDepts = cacheList();
        Optional<SysDept> optional = sysDepts.stream().filter(data -> data.getDeptId().equals(deptId)).findFirst();
        if (optional.isPresent()) {
            //获取到部门对象
            SysDept dept = optional.get();
            //分隔路径信息
            String[] ancestorIds = dept.getAncestors().split(",");
            //构建名称路径集合
            ArrayList<String> namePath = Lists.newArrayList();
            return Arrays.stream(ancestorIds)
                    .map(Long::parseLong)
                    .map(id -> sysDepts.stream()
                            //过滤出上级
                            .filter(element -> element.getDeptId().equals(id))
                            .map(element -> element.getDeptName())
                            .findFirst().orElse(null)
                    ).filter(Objects::nonNull)
                    .collect(Collectors.joining("/"));


        }
        return null;
    }

    public static Long nameToId(String name) {
        if(ObjectUtils.isEmpty(name)){
            return null;
        }
        Optional<SysDept> optional = cacheList().stream().filter(data -> data.getDeptName().equals(name)).findFirst();
        if(optional.isPresent()){
            return optional.get().getDeptId();
        }
        return null;
    }
}
